package com.kuang.web.controller;

import com.kuang.web.dao.DepartmentDao;
import com.kuang.web.dao.EmployeeDao;
import com.kuang.web.pojo.Department;
import com.kuang.web.pojo.Employee;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

@Controller
public class EmploeeController {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;
    //查询所有员工
    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }
    //添加员工的表单页面(用Get)
    @GetMapping("/add")
    public String add(Model model){
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "/emp/add";
    }
    //添加员工
    @PostMapping("/addEmp")
    public String add(Employee employee){
        System.out.println("save=>"+employee);
        employeeDao.save(employee);
        return "redirect:/emps";
    }
    //修改页面
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id")int id, Model model){
        Employee employeeById = employeeDao.getEmployeeById(id);
        model.addAttribute("empByID",employeeById);
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "/emp/edit";
    }
    //修改
    @RequestMapping("/update")
    public String editok(Employee employee){
        employeeDao.update(employee);
        return "redirect:/emps";
    }
    //删除
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable int id){
        employeeDao.delete(id);
        return "redirect:/emps";
    }


}
